const A = getApp();
var setTime;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    createTime: '',
    commentList: [],
    comment: '',
    id: '',
    flag: '',
    url:''
  },
  // 获取评论
  bindComment(e) {
    this.setData({
      comment: e.detail.value
    })
  },

  onfirmComment(e) {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower', })
    }
    let value = e.detail.value;
    let flag = this.data.flag;
    let config = {
      content: value,
      originType: flag,
      originId:this.data.id
    }
    A.updata.addCommentDetail(config).then(res => {
      console.log(res);
      if(res.code == 200) {
        console.log('评论成功');
        this.setData({
          comment: ''
        })
        this.getCommentDetailVo();
      }
    })
  },

  // 获取积分
  addIntegraHistory() {
    let config = {
      originId: this.data.id,
      origin: 'EVERY_DAY_STUDY'
    }
    A.updata.addIntegraHistory(config).then(res => {
      console.log('获取积分');
      console.log(res);
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let flag = options.flag || '';
    this.getAritlesDetail(id, flag);
    this.setData({
      id,
      flag
    })
    this.getCommentDetailVo();
    setTime = setTimeout(() => {
      this.addIntegraHistory();
    }, 10000)

  },
  // 获取文章详情
  getAritlesDetail(id, flag) {
    let config = {
      id
    }
    let that = this;
    A.updata.getAritlesDetail(config).then(res => {
      this.setData({
        title: res.results.title,
        content: res.results.content,
        createTime: res.results.createTime,
        id: res.results.id,
        url: "https://app.wjgqt.com/article/html?id="+id
      })
      console.info("url :" + this.data.url)
    })

  },

  // 获取评论列表
  getCommentDetailVo() {
    // 评论类型(0：每日学习，1：教育直通车，2：视频)
    let id = this.data.id;
    let flag = this.data.flag;
    let config = {
      originId: id,
      originType: flag,
      pageNum: 1,
      pageSize: 100
    }
    console.log(config);
    A.updata.getCommentDetailVo(config).then(res => {
      console.log('获取评论列表');
      console.log(res);
      this.setData({
        commentList: res.list
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onUnload: function () {
    clearTimeout(setTime);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})